/* util */
import { isOpenData } from '@src/util/platform'
import { uuid } from 'pub-bbx-utils'
import Log from '@src/util/log'
import { isPlanObject } from '@src/util/type'
/* model */
import Field from '@model/entity/Field'
import Auth from '@model/entity/Auth'
import RootWindowInitData from '@model/interface/initData/RootWindowInitData'
/* service */
import FormService from '@service/FormService'
import AuthService from '@service/AuthService'
/* util */
import { getRootWindowInitData } from '@src/util/window'
import { getThemeColor } from '@src/util/business/color'
/* vue */
import { defineComponent } from 'vue'

type GlobalMixinProps = {
  
}

type GlobalMixinData = {
  isOpenData: boolean;
}

export type GlobalMixinComputedOptions = {
  globalRootWindowInitData: () => RootWindowInitData;
  globalLoginUserAuth: () => Auth;
  globalIsHaveCallCenterViewDetailAuth: () => boolean;
  globalIsHaveImViewDetailAuth: () => boolean;
  globalIsHaveUserViewDetailAuth: () => boolean;
  globalIsHaveTaskPlanViewDetailAuth: () => boolean;
  getThemeColor: () => string;
}

type GlobalMixinMethodsOptions = {
  uuid: () => string;
  getPerformanceShowTooltip: (field: Field) => boolean;
  getProductTypeListShowTooltip: (field: Field) => boolean;
  getSparePartPersonalUserNameShowTooltip: (field: Field) => boolean;
  getCommonListShowTooltip: (field: Field) => boolean;
  getEventListShowTooltip: (field: Field) => boolean;
  getProductListShowTooltip: (field: Field) => boolean;
  globalFormSubmissionDataProcessingHandler: (fields: Field[], formData: Record<string, any>) => Record<string, any>;
}

export type GlobalMixinVM =  GlobalMixinComputedOptions & GlobalMixinMethodsOptions

export default defineComponent<GlobalMixinProps, GlobalMixinData, GlobalMixinData, GlobalMixinComputedOptions, GlobalMixinMethodsOptions>({
  data() {
    return {
      isOpenData,
    }
  },
  computed: {
    globalRootWindowInitData() {
      return getRootWindowInitData()
    },
    /** 
     * @description 全局登录用户权限
    */
    globalLoginUserAuth(): Auth | Record<string, number> {
      return (this.globalRootWindowInitData?.user?.auth || {}) as Auth
    },
    globalIsHaveCallCenterViewDetailAuth() {
      return AuthService.isHaveCallCenterViewDetailAuth(this.globalLoginUserAuth)
    },
    globalIsHaveImViewDetailAuth() {
      return AuthService.isHaveImViewDetailAuth(this.globalLoginUserAuth)
    },
    globalIsHaveUserViewDetailAuth() {
      return AuthService.isHaveUserViewDetailAuth(this.globalLoginUserAuth)
    },
    globalIsHaveTaskPlanViewDetailAuth() {
      return AuthService.isHaveTaskPlanViewDetailAuth(this.globalLoginUserAuth)
    },
    /**
     * 全局样式主题颜色
     */
    getThemeColor() {
      return getThemeColor()
    }
  },
  methods: {
    uuid() {
      return uuid()
    },
    getPerformanceShowTooltip(column: Field) {
      
      if (!this.isOpenData) return true
      
      return column.field != 'createUser' && column.field != 'carbonCopy' && column.field != 'users'
    },
    getProductTypeListShowTooltip(column: Field) {
      
      const defaultShow = column.fieldName !== 'productPic' && column.fieldName !== 'thumbnail'
      
      if (!this.isOpenData) return defaultShow
      
      return column.formType != 'user' && defaultShow
    },
    getSparePartPersonalUserNameShowTooltip(column: Field) {
      
      if (!this.isOpenData) return true
      
      return false
    },
    getCommonListShowTooltip(column: Field) {
      const defaultShow = column.fieldName !== 'name'
      
      if (!this.isOpenData) return defaultShow
      
      return column.formType != 'user' && defaultShow
    },
    getEventListShowTooltip(column: Field) {
      
      const defaultShow = column.fieldName !== 'eventNo'
      
      if (!this.isOpenData) return defaultShow
      
      return column.formType != 'user' && defaultShow
    },
    getProductListShowTooltip(column: Field) {
      
      const defaultShow = column.field !== 'name' && column.fieldName !== 'productPic' && column.field != 'customerTag'
      
      if (!this.isOpenData) return defaultShow
      
      return column.formType != 'user' && defaultShow
    },
    globalFormSubmissionDataProcessingHandler(fields = [], formData: Record<string, any> = {}) {
      try {
        
        if (isPlanObject(formData?.attribute)) {
          const isAttribute = true
          return FormService.formSubmissionDataProcessingHandler(fields, formData, isAttribute)
        }
        
        return FormService.formSubmissionDataProcessingHandler(fields, formData)
        
      } catch (error) {
        
        Log.error(error, this.globalFormSubmissionDataProcessingHandler.name)
        
        return formData
      }
    }
  }
})
